import {RemoteDeviceListManager} from '../model/RemoteDeviceListManager'
import ServiceExtensionContext from "D:/Work/HuaweiOHOS_SDK/OpenHarmonySDK_2022.12.07_fixed/9/ets/api/application/ServiceExtensionContext";
import rpc from '@ohos.rpc';

const TAG: string = "BtConnector[RemoteDeviceConnect]";
const REQUEST_CODE = 251 //'ы'

export class RemoteDeviceConnect {
    private connectionId = null
    private context: ServiceExtensionContext = null
    private remoteDeviceListManager: RemoteDeviceListManager = new RemoteDeviceListManager()
    private remoteConnection: rpc.IRemoteObject = null

    constructor(context:ServiceExtensionContext) {
        console.log(`${TAG} constructor`);
        this.context = context
    }

    startConnection() {
        if (this.remoteConnection !== null) return
        console.info(`${TAG} startConnection`)
        let devList = this.remoteDeviceListManager.getList()
        if (devList !== undefined && devList.length !== undefined) {
            if (devList.length > 0) {
                this.connect(devList[0].deviceId)
            }
        }
        setTimeout(() => {this.startConnection()}, 5000)
    }

    getStatus(): Boolean {
        return this.remoteConnection !== null
    }

    connect(deviceId: string) {
        /*
        this.context.startAbility({
            deviceId: deviceId,
            bundleName: 'ohos.samples.distributedcalc',
            abilityName: 'ohos.samples.distributedcalc.MainAbility',
        }).then(() => {
            console.info(`${TAG} startApplication promise success`)
        }, (err) => {
            console.error(`${TAG} startApplication promise error: ${JSON.stringify(err)}`)
        })
        /*/
        console.info(`${TAG} connect to ${deviceId}`)
        let want = {
            "deviceId": deviceId,
            "bundleName": "com.ohos.callui",
            "abilityName": "com.ohos.callui.ServiceAbility"
        };
        let options = {
            onConnect(elementName, remote) {
                console.info(`${TAG} onConnect callback`);
                if (remote === null) {
                    console.info(`${TAG} onConnect remote is null`);
                    return;
                }
                this.remoteConnection = remote
            },
            onDisconnect(elementName) {
                console.info(`${TAG} onDisconnect callback`)
            },
            onFailed(code) {
                console.info(`${TAG} onFailed callback ${code}`)
            }
        }
        console.info(`${TAG} connect start ${this.context}`)
        this.connectionId = this.context.connectServiceExtensionAbility(want, options);
        console.info(`${TAG} connect end`)
        // */
    }

    send() {
        if (this.remoteConnection === null) {
            return
        }
        /*
        let option = new rpc.MessageOption();
        let data = new rpc.MessageParcel();
        let reply = new rpc.MessageParcel();
        data.writeInt(100);
        data.writeInt(200);

        // @param code 表示客户端发送的服务请求代码。
        // @param data 表示客户端发送的{@link MessageParcel}对象。
        // @param reply 表示远程服务发送的响应消息对象。
        // @param options 指示操作是同步的还是异步的。
        //
        // @return 如果操作成功返回{@code true}； 否则返回 {@code false}。
        this.remoteConnection.sendRequest(REQUEST_CODE, data, reply, option).then((ret) => {
            let msg = reply.readInt();
            console.info(`${TAG} sendRequest ret:${ret} msg:${msg}`);
        }).catch((error) => {
            console.info(`${TAG} sendRequest failed`);
        });// */
    }
}